import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk  # 导入PIL库用于图像处理
import os


class ShowImagePage(tk.Frame):
    """显示图片页面"""
    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller
        
        # 创建标题
        title = ttk.Label(self, text="图片展示", font=("微软雅黑", 16, "bold"))
        title.pack(pady=20)
        
        # 加载并显示图片
        self.load_and_display_image()
        
    def load_and_display_image(self):
        try:
            # 获取当前脚本所在目录
            script_dir = os.path.dirname(os.path.abspath(__file__))
            parent_dir  = os.path.dirname(script_dir)
            # 拼接图片的绝对路径
            image_path = os.path.join(parent_dir, "resources", "image", "1.png")

            # 替换为你的图片路径
            # image_path = "../resources/image/1.png"
            
            # 打开图片
            image = Image.open(image_path)
            
            # 调整图片大小以适应窗口（可选）
            max_width = 800
            max_height = 600
            image.thumbnail((max_width, max_height))
            
            # 将图片转换为Tkinter可用的格式
            self.photo = ImageTk.PhotoImage(image)
            
            # 创建显示图片的标签
            image_label = ttk.Label(self, image=self.photo)
            image_label.pack(pady=10)
            
            # 添加图片路径标签
            path_label = ttk.Label(self, text=f"图片路径: {image_path}", font=("微软雅黑", 10))
            path_label.pack(pady=5)
            
        except Exception as e:
            # 处理可能的错误
            error_label = ttk.Label(self, text=f"加载图片时出错: {str(e)}", foreground="red")
            error_label.pack(pady=20)

# 创建主窗口
if __name__ == "__main__":
    root = tk.Tk()
    root.title("图片显示")
    root.geometry("900x700")
    
    # 创建并放置显示图片的页面
    app = ShowImagePage(root, None)
    app.pack(fill="both", expand=True)
    
    root.mainloop()